﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raise3000.Code.Foundation;

namespace Raise3000.Code.HandHistoryLoader
{
    public class HandHistoryParserHelper
    {
        private List<string> playersInHand_ = null;

        // the unit is cent: (e.g - sb = 5; bb = 10)
        private List<long> chipsCommited_ = null;        

        private bool flopSeen_ = false;

        private bool allPlayersFolded_ = true;

        private bool allinSituation_ = false;

        public bool FlopSeen
        {
            get
            {
                return flopSeen_;
            }
            set
            {
                flopSeen_ = value;
            }

        }

        public bool AllFold
        {
            get
            {
                return allPlayersFolded_;
            }
            set
            {
                allPlayersFolded_ = value;
            }
        }

        public bool AllinSituation
        {
            get
            {
                return allinSituation_;
            }
            set
            {
                allinSituation_ = value;
            }
        }

        public HandHistoryParserHelper(int numberOfPlayers)
        {
            playersInHand_ = new List<string>(numberOfPlayers);
            chipsCommited_ = new List<long>(numberOfPlayers);            
        }

        public void AddPlayerAndChips(string player, long amountOfChips)
        {
            int indexOf = playersInHand_.IndexOf(player);
            if (indexOf == -1)
            {
                playersInHand_.Add(player);
                chipsCommited_.Add(amountOfChips);
            }
            else
            {
                // the player is already in hand.
                chipsCommited_[indexOf] += amountOfChips;
            }

        }

        public void RemovePlayer(string player)
        {
            int indexOf = playersInHand_.IndexOf(player);

            if (indexOf != -1)
            {
                playersInHand_.RemoveAt(indexOf);
                chipsCommited_.RemoveAt(indexOf);                
            }
        }

        // Test if all player has matched with the last raise.
        private bool IsBetMatchedByAllPlayers()
        {
            bool isMatch = true;

            long chips = chipsCommited_[0];
            for (int idx = 1; idx < playersInHand_.Count && isMatch; ++idx)
            {
                isMatch = chipsCommited_[idx] == chips;
            }

            return isMatch;
        }

        public bool IsTherePlayerLeftToAct()
        {
            bool isTherePlayerToAct = (playersInHand_.Count >= 2) && !IsBetMatchedByAllPlayers();
            return isTherePlayerToAct;
        }

        public int NumberOfPlayerInHand()
        {
            return playersInHand_.Count;
        }

        public bool IsTokenAPlayerName(string token)
        {
            int indexOf = playersInHand_.IndexOf(token);
            return (indexOf != -1);
        }

        public ActionEnum ConvertToActionEnum(string move)
        {
            ActionEnum convertedMove = ActionEnum.eUNKNOWN;

            if (move == "Folds")
            {
                convertedMove = ActionEnum.eFOLD;
            }
            else if (move == "Checks")
            {
                convertedMove = ActionEnum.eCHECK;
            }
            else if (move == "Calls")
            {
                convertedMove = ActionEnum.eCALL;
            }
            else if (move == "Bets")
            {
                convertedMove = ActionEnum.eBET;
            }
            else if (move == "Raises")
            {
                convertedMove = ActionEnum.eRAISE;
            }

            return convertedMove;
        }
    }
}
